package com.sinosoft.emplmis.application.web.dto;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.sinosoft.framework.core.api.domain.UserEntity;
import com.sinosoft.framework.core.common.utils.ObjectUtil;

/**
 * 用户数据传输对象
 * @author xiaoxiong
 *
 */
public class UserData implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	
	private Integer id;
	
	private String loginName;
	
	/**
	 * 当前密码
	 */
	private String currentPwd;
	
	/**
	 * 新密码
	 */
	private String newPwd;
	
	private String name;
	
	private Short gender;
	
	private String genderName;
	
	private String description;
	
	private Integer loginCount;
	
	private Timestamp previousVisit;
	
	private Timestamp lastVisit;
	
	private String previousVisit1;

	private String lastVisit1;
	
	private List<RoleData> roles;
	
	private UserOrgsData userOrgs;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getCurrentPwd() {
		return currentPwd;
	}

	public void setCurrentPwd(String currentPwd) {
		this.currentPwd = currentPwd;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Short getGender() {
		return gender;
	}

	public void setGender(Short gender) {
		this.gender = gender;
	}

	public String getGenderName() {
		return genderName;
	}

	public void setGenderName(String genderName) {
		this.genderName = genderName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Integer getLoginCount() {
		return loginCount;
	}

	public void setLoginCount(Integer loginCount) {
		this.loginCount = loginCount;
	}

	public Timestamp getPreviousVisit() {
		return previousVisit;
	}

	public void setPreviousVisit(Timestamp previousVisit) {
		this.previousVisit = previousVisit;
	}

	public Timestamp getLastVisit() {
		return lastVisit;
	}

	public void setLastVisit(Timestamp lastVisit) {
		this.lastVisit = lastVisit;
	}

	public String getPreviousVisit1() {
		return previousVisit1;
	}

	public void setPreviousVisit1(String previousVisit1) {
		this.previousVisit1 = previousVisit1;
	}

	public String getLastVisit1() {
		return lastVisit1;
	}

	public void setLastVisit1(String lastVisit1) {
		this.lastVisit1 = lastVisit1;
	}
	
	public List<RoleData> getRoles() {
		return roles;
	}

	public void setRoles(List<RoleData> roles) {
		this.roles = roles;
	}
	
	public UserOrgsData getUserOrgs() {
		return userOrgs;
	}

	public void setUserOrgs(UserOrgsData userOrgs) {
		this.userOrgs = userOrgs;
	}

	/**
	 * 数据转换
	 * @param userData
	 * @param userEntity
	 */
	public void toUserData(UserData userData, UserEntity userEntity) {
		try {
			ObjectUtil.oldToNew(userData, userEntity, "serialVersionUID");
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.setConvertFields(userData, userEntity);
	}
	
	/**
	 * 数据转换
	 * @param userData
	 * @param userEntity
	 * @param roles
	 */
	public void toUserData(UserData userData, UserEntity userEntity, List<RoleData> roleDatas,UserOrgsData userOrgsData) {
		try {
			ObjectUtil.oldToNew(userData, userEntity, "serialVersionUID");
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.setConvertFields(userData, userEntity);
		userData.setRoles(roleDatas);
		userData.setUserOrgs(userOrgsData);
	}
	
	/**
	 * 设置公用的转换字段
	 */
	private void setConvertFields(UserData userData, UserEntity userEntity) {
		switch(userEntity.getGender()) {
			case 0:
				userData.setGenderName("男");
				break;
			case 1:
				userData.setGenderName("女");
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date date = new Date();
		if(userEntity.getPreviousVisit() != null){
			date = userEntity.getPreviousVisit();
			userData.setPreviousVisit1(sdf.format(date));
		}
		if(userEntity.getLastVisit() != null) {
			date = userEntity.getLastVisit();
			userData.setLastVisit1(sdf.format(date));
		}
	}
}
